/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.util;

import axescon.xacml.engine.BasePolicyStore;
import axescon.xacml.model.policy.PolicyObject;
import axescon.xacml.api.XacmlException;

import java.io.*;
import java.util.logging.Logger;

/**
 * @author argyn
 *         Date: Oct 6, 2005
 *         Responsibilities: extends BasePolicyStore by adding capability of
 * look ups from XML files.
 */
public class FilePolicyStore extends BasePolicyStore {
    static Logger log = Logger.getLogger("axescon.xacml.util.FilePolicyStore");

    public void setPolicies(String[] fileNames) throws XacmlException {
        for (int i = 0; i < fileNames.length; i++) {
            addPolicy(new File(fileNames[i]));
        }
    }

    public void addPolicy(String fn) throws XacmlException {
        addPolicy(new File(fn));
    }

    public void setPolicies(File[] pfs) throws XacmlException {
        for (int i = 0; i < pfs.length; i++) {
            addPolicy(pfs[i]);
        }
    }

    public void addPolicy(File pf) throws XacmlException {
        Reader r = null;
        try {
            r = new FileReader(pf);
        } catch (FileNotFoundException e) {
            log.config("can't open reader for policy file: "+pf);
            throw new XacmlException("policy file: "+pf,e,XacmlException.ERR_CODE_CONFIG_POLICY_READER_IO);
        }
        PolicyObject po = null;
        try {
            po = getPolicyFactory().getPolicyObject(r);
        } catch (XacmlException e) {
            log.severe("can't parse policy file: "+pf);
            throw e;
        }
        addPolicy(po);
        try {
            r.close();
        } catch (IOException e) {
            log.config("can't close file reader for policy file: "+pf);
        }
    }

}
